package com.company.crm.settings.web.controller;

import com.company.crm.commons.constants.Constants;
import com.company.crm.commons.domain.ReturnObject;
import com.company.crm.commons.util.UUIDUtils;
import com.company.crm.settings.domain.DicType;
import com.company.crm.settings.domain.DicValue;
import com.company.crm.settings.service.DicTypeService;
import com.company.crm.settings.service.DicValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 作者：王磊栋
 * 2021/4/15
 */
@Controller
public class DictionaryController {

    @Autowired
    private DicTypeService dicTypeService;

    @RequestMapping("/settings/dictionary/index")
    public String skipDictionaryPage(){
       return "settings/dictionary/index";
    }

    @RequestMapping("/settings/dictionary/type/index.do")
    //model和request很像都是请求作用域
    public String loadDictionaryType(Model model){
        List<DicType> dicTypes = dicTypeService.selectAllDicType();
        model.addAttribute("dicTypeList",dicTypes);
        return "settings/dictionary/type/index";
    }

    //跳转到创建字典类型页面
    @RequestMapping("/settings/dictionary/type/toSave.do")
    public String toSave(){
        return "settings/dictionary/type/save";
    }

    //当跳转到添加字典类型的时候，处理前段发送的ajax的请求
    //当输入code的时候，这个输入文本框失去焦点的时候，首先进行判断这个code时候在数据库中存在。

    @RequestMapping("/settings/dictionary/type/checkCode.do")
    @ResponseBody()
    public Object checkCode(String code){
        //进行查询
        DicType dicType = dicTypeService.selectDicTypeByCode(code);
        ReturnObject ro = new ReturnObject();
        if (dicType == null){
            //证明这个code可以用吧
            ro.setCode(Constants.LOGIN_CODE_SUCCESS);
        }else {
            ro.setCode(Constants.LOGIN_CODE_FAIL);
            ro.setMessage("当前编码已经存在！");
        }
        return ro;
    }

    //当code进行验证数据没有重复的时候，这个时候才能点击保存按钮
    //这个保存按钮，也是发送ajax的请求,
    //处理保存时间的ajax请求。拿到并把请求参数拿过来
    @RequestMapping("/settings/dictionary/type/saveCreateDicType.do")
    @ResponseBody
    //正好可以封装到dictype对象中
    public Object creatDicType(DicType dicType){
        ReturnObject ro = new ReturnObject();
        int result = dicTypeService.insertDicType(dicType);
        if (result>0){
             ro.setCode(Constants.LOGIN_CODE_SUCCESS);
        }else{
            ro.setCode(Constants.LOGIN_CODE_FAIL);
            ro.setMessage("添加失败失败");
        }
        return ro;
    }
    //编辑功能携带参数跳转编辑页
    @RequestMapping("/settings/dictionary/type/editDicType.do")
    public String editeDicType(String code,Model model){
        DicType dicType = dicTypeService.selectDicTypeByCode(code);
        if (dicType!=null){
               model.addAttribute("dicType", dicType);
        }
        return "settings/dictionary/type/edit";
    }

    //然后通过对确认键绑定事件。获取参数值，并通过ajax的形式把参数传递到后端。
    @RequestMapping("/settings/dictionary/type/saveEditDicType.do")
    @ResponseBody
    public Object updateDicType(DicType dicType){
        ReturnObject ro = new ReturnObject();
        int result = dicTypeService.updateDicType(dicType);
        if (result>0){
            ro.setCode(Constants.LOGIN_CODE_SUCCESS);
        }else{
            ro.setCode(Constants.LOGIN_CODE_FAIL);
            ro.setMessage("更新失败");
        }
        return ro;
    }

    //批量删除
    //第一点：在点击删除之前需要进行jquery的判断
    //第二点：当没有选着任何元素的时候，那么就弹出必须弹出必须选着想要删除的内容。
    //第三点：在进行删除的时候，必须进行进行再次确认。window.confirm("确定删除吗？") 返回值是true或者false
    //第四点：收集参数，发送请求，处理请求发回来的数据。
    @RequestMapping("/settings/dictionary/type/deleteDicTypeByCodes.do")
    @ResponseBody
    public Object deleteDicTypeByCodes(String[] code){
        ReturnObject ro = new ReturnObject(); //
        int result = dicTypeService.deleteDicType(code);
        if (result>0){
            ro.setCode(Constants.LOGIN_CODE_SUCCESS);
        }else {
            ro.setCode(Constants.LOGIN_CODE_FAIL);
            ro.setMessage("删除失败");
        }
        return ro;
    }


    /*                  处理数据字典值                */

    @Autowired
    private DicValueService dicValueService;

    @RequestMapping("/settings/dictionary/value/index.do")
    public String loadDictionaryValue(Model model){
        List<DicValue> dicValues = dicValueService.selectAllDicValues();
        model.addAttribute("dicValueList", dicValues);
        return "settings/dictionary/value/index";
    }

    //创建一个字典值
    @RequestMapping("/settings/dictionary/value/toSave.do")
    public String toSaveDicValue(Model model){
        List<DicType> dicTypes = dicTypeService.selectAllDicType();
        model.addAttribute("dicTypeList", dicTypes);
        return "settings/dictionary/value/save";
    }

    //当前端点击保存的时候，发送请求。后端处理
    @RequestMapping("/settings/dictionary/value/saveCreateDicValue.do")
    @ResponseBody
    public Object saveCreateDicValue(DicValue dicValue){
        ReturnObject ro = new ReturnObject();
        dicValue.setDelFlag(1);
        dicValue.setId(UUIDUtils.getUUID());
        int result = dicValueService.insertDicValues(dicValue);
        if(result>0){
            ro.setCode(Constants.LOGIN_CODE_SUCCESS);
        }else {
            ro.setCode(Constants.LOGIN_CODE_FAIL);
            ro.setMessage("创建失败,请重试！");
        }
        return ro;
    }

    //当首页点击编辑按钮的时候。需要进行
    @RequestMapping("/settings/dictionary/value/editDicValue.do")
    public String editDicValue(String id,Model model){
        //通过id来查找
        DicValue dicValue = dicValueService.selectDicValuesById(id);
        model.addAttribute("dicValue", dicValue);
        return "settings/dictionary/value/edit";
    }

    //当点击了更新的时候，、
    //首先绑定事件，收集参数，发送ajax请求
    /*
                        id:id,
						value:value,
						text:text,
						orderNo:orderNo
	*/
    @RequestMapping("/settings/dictionary/value/saveEditDicValue.do")
    @ResponseBody
    public Object saveEditDicValue(DicValue dicValue){
        ReturnObject ro = new ReturnObject();
        //dicValueService
        int result = dicValueService.updateDicValue(dicValue);
        if (result>0){
            ro.setCode(Constants.LOGIN_CODE_SUCCESS);
        }else {
            ro.setCode(Constants.LOGIN_CODE_FAIL);
            ro.setMessage("更新失败");
        }
        return ro;
    }

    //点击删除，进行逻辑删除
    @RequestMapping("/settings/dictionary/value/deleteDicValueByIds.do")
    @ResponseBody()
    public Object deleteDicValueByIds(String[] id){
        ReturnObject ro = new ReturnObject();
        //dicValueService
        int result = dicValueService.logicDeleteByID(id);
        if (result>0){
            ro.setCode(Constants.LOGIN_CODE_SUCCESS);
        }else {
            ro.setCode(Constants.LOGIN_CODE_FAIL);
            ro.setMessage("更新失败");
        }
        return ro;
    }
}
